using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using User.Models;
using User.Service;
using UserLogicCore.Logic;
using UserSorce.Entity.Models;

namespace User.Controllers
{
    public class TypeController : Controller
    {
        TypeService _typectx = new TypeService();
        TypeServiceModels _typeModel = new TypeServiceModels();
        private UserService _ctx = new UserService();
        private UserServiceModels _userInformation = new UserServiceModels();
        private MatterService _Matter = new MatterService();
        public static int UserId;

        /// <summary>
        /// 添加清单类型
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddTypeName(int userId,string typeName)
        {var user = GetUser(userId);
            if (typeName == null)
            {
                ViewData["Message"] = "输入内容不能为空";
            }
            else
            {
             _typectx.AddType(userId, typeName);
                user = GetUser(userId);
            }
            return View("AddTypeName",user);
        }
        /// <summary>
        /// 添加清单类型
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult AddType()
        {
            var user = GetUser(UserId);
            return View("AddTypeName", user);
        }

        /// <summary>
        /// 编辑清单类型
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="typeId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult EditType(int userId,int typeId)
        {
           
            Users userData = _ctx.GetData(userId);
            var type = _typeModel.TurnOffTypeData(userData, typeId);
            return View("EditType",type);
        }
        /// <summary>
        /// 修改清单类型名称
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="typeId"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult ChangeType(int userId,int typeId,string typeName)
        {
            _typectx.ChangeTypeName(typeId, typeName);
            Users userData = _ctx.GetData(userId);
            var type = _typeModel.TurnOffTypeData(userData, typeId);
            return View("EditType", type);
        }
        /// <summary>
        /// 查看所有清单类型
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Type(int userId)
        {
            var user = GetUser(userId);
            return View("AddType", user);
        }
        /// <summary>
        /// 删除事项
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="matterId"></param>
        /// <returns></returns>
         [HttpGet]
        public IActionResult DeleteMatters(int userId,int matterId)
        {
            _Matter.DeleteEvent(matterId);
            var user = GetUser(userId);
            return View("AddType", user);
        }

        /// <summary>
        /// 还有3天过期的事项
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult UpcomingOverTime()
        {
            var mattersModel = GetMatters();
            return View("UpcomingOverTime",mattersModel);
        }
        [HttpGet]
        public IActionResult CompletMatter(int matterId)
        {
            _Matter.ChangeEventSate(true, matterId);
            var mattersModel = GetMatters();
            return View("UpcomingOverTime", mattersModel);
        } 
        /// <summary>
        /// 得到还有3天过期的事项
        /// </summary>
        /// <returns></returns>
        public List<MattersModels> GetMatters()
        {
            MatterServiceModels matterSerivce = new MatterServiceModels();
            var user = GetUser(UserId);
            var matters = user.Matter.Where(p => p.OverTime <= DateTime.Now.AddDays(3) && p.IsFinished == false).ToList();
            var mattersModel = matterSerivce.TurnOffMatter(matters);
             var list=mattersModel.OrderBy(a => a.OverTime).ToList();
            return list;
        }
        /// <summary>
        /// 得到用户的数据
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public UserModels GetUser(int userId)
        {
            Users userData = _ctx.GetData(userId);
            var user = _userInformation.TurnOffUserData(userData);
            return user;
        }

    }
}